package com.springboot.tool;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * 分页类
 *
 * @author Victor
 */
public class PageTool {
    public int firstRow;//起始页【查询起始页】
    public int listRow;//每页记录数
    public int totalRows;//总记录数
    public int totalPages;//总页数，需要计算
    public int nowPage;//当前页：如第一页，则下页请求为第二页
    public int prePage;//上一页
    public int nextPage;//下一页
    public StringBuffer url;


    /**
     * @param totalRows 总记录
     * @param listRow   每页条数
     * @param nowPage   当前页1
     */
    public PageTool(int totalRows, int listRow, int currentPage, StringBuffer url) throws Exception {
        int nowPage;
        if (totalRows < 1) {
            throw new Exception("总记录数不能小于零");
        }

        if (listRow < 1) {
            throw new Exception("每页记录数不能小于零");
        }

        if (currentPage < 1) {
            nowPage = 1;
        } else {
            nowPage = currentPage;
        }

        this.totalRows = totalRows;
        this.listRow   = listRow;
        this.firstRow  = listRow * (nowPage - 1);

        //计算总页数
        this.totalPages = (int) Math.ceil((double) totalRows / listRow);
        this.nowPage    = nowPage;
        if (this.totalPages <= this.nowPage) {
            this.firstRow = listRow * (totalPages - 1);
        }

        //上一页
        this.prePage = this.nowPage - 1;
        if (this.prePage <= 0) {
            this.prePage = 1;
        }

        //下一页
        this.nextPage = this.nowPage + 1;
        if (this.nextPage >= this.totalPages) {
            this.nextPage = this.totalPages;
        }

        this.url = url;

    }


    /**
     * .rows{
     * display: block;
     * height: 30px;
     * line-height: 30px;
     * float: left;
     * }
     * .pages a{
     * height: 28px;
     * border:1px solid #CCCCCC;
     * text-decoration: none;
     * display: block;
     * text-align: center;
     * line-height: 30px;
     * float: left;
     * margin-left: 5px;
     * color: #333333;
     * border-radius: 2px;
     * }
     * .number{
     * padding-left: 10px;
     * padding-right: 10px;
     * }
     * .currentPage{
     * background: #F0882C;
     * height: 30px;
     * text-decoration: none;
     * display: block;
     * text-align: center;
     * line-height: 30px;
     * float: left;
     * margin-left: 5px;
     * color: #333333;
     * border-radius: 2px;
     * padding-left:8px;
     * padding-right:8px;
     * color: white;
     * font-weight: bold;
     * }
     * .pages a:hover{
     * background: #DDDDDD;
     * }
     * .fristPage,.lastPage{
     * width: 40px;
     * }
     * .prePage,.nextPage{
     * width: 58px;
     * }
     *
     * @return
     */
    public Map<String, String> show() {
        String stylesPage = "<style type=\"text/css\">\n" +
                "\t.rows{\n" +
                "\t\tdisplay: block;\n" +
                "\t\theight: 30px;\n" +
                "\t\tline-height: 30px;\n" +
                "\t\tfloat: left;\n" +
                "\t}\n" +
                "\t.pages a{\n" +
                "\t\theight: 28px;\n" +
                "\t\tborder:1px solid #CCCCCC; \n" +
                "\t\ttext-decoration: none;\n" +
                "\t\tdisplay: block;\n" +
                "\t\ttext-align: center;\n" +
                "\t\tline-height: 30px;\n" +
                "\t\tfloat: left;\n" +
                "\t\tmargin-left: 5px;\n" +
                "\t\tcolor: #333333;\n" +
                "\t\tborder-radius: 2px;\n" +
                "\t}\n" +
                "\t.number{\n" +
                "\t\tpadding-left: 10px;\n" +
                "\t\tpadding-right: 10px;\n" +
                "\t}\n" +
                "\t.currentPage{\n" +
                "\t\tbackground: #F0882C;\n" +
                "\t\theight: 30px;\n" +
                "\t\ttext-decoration: none;\n" +
                "\t\tdisplay: block;\n" +
                "\t\ttext-align: center;\n" +
                "\t\tline-height: 30px;\n" +
                "\t\tfloat: left;\n" +
                "\t\tmargin-left: 5px;\n" +
                "\t\tcolor: #333333;\n" +
                "\t\tborder-radius: 2px;\n" +
                "\t\tpadding-left:8px;\n" +
                "\t\tpadding-right:8px;\n" +
                "\t\tcolor: white;\n" +
                "\t\tfont-weight: bold;\n" +
                "\t}\n" +
                "\t.pages a:hover{\n" +
                "\t\tbackground: #DDDDDD;\n" +
                "\t}\n" +
                "\t.fristPage,.lastPage{\n" +
                "\t\twidth: 40px;\n" +
                "\t}\n" +
                "\t.prePage,.nextPage{\n" +
                "\t\twidth: 58px;\n" +
                "\t}\n" +
                "</style>";
        String totalPg          = "<div class=\"pages\" style=\"margin-left:50px\"><span class=\"rows\">共" + totalRows + "条记录</span>";
        String firstPg          = "<a href=\"" + url + "?p=1\" class=\"prePage\">首页</a>";
        String prePg            = "<a href=\"" + url + "?p=" + prePage + "\" class=\"prePage\">上一页</a>";
        String nextPg           = "<a href=\"" + url + "?p=" + nextPage + "\" class=\"nextPage\">下一页</a>";
        String lastPg           = "<a href=\"" + url + "?p=" + totalPages + "\" class=\"lastPage\">末页</a></div>";
        String numberPage       = currentNumber();
        String shows            = stylesPage + totalPg + firstPg + prePg + numberPage + nextPg + lastPg;
        Map<String, String> map = new HashMap();
        /**
         * 分页参数
         */
        map.put("stylePage", stylesPage);
        map.put("totalRows", totalPg);
        map.put("firstPage", firstPg);
        map.put("prePage", prePg);
        map.put("nextPage", nextPg);
        map.put("lastPage", lastPg);
        /**
         * 分页
         */
        map.put("shows", shows);
        return map;
    }

    /**
     * 数字页码
     * rollPage:所显示数字页码数；
     *
     * @return
     */
    public String currentNumber() {
        int page;
        int i;
        String numberPage     = "";
        double rollPage       = 5;
        double half_page      = rollPage / 2;
        double ceil_half_page = Math.ceil(half_page);
        for (i = 1; i <= rollPage; i++) {
            if (nowPage - half_page <= 0) {
                page = i;
            } else if ((nowPage + half_page - 1) >= totalPages) {
                page = (int) (totalPages - rollPage + i);
            } else {
                page = (int) (nowPage - ceil_half_page + i);
            }
            if (page > 0 && page != nowPage) {
                if (page <= totalPages) {
                    numberPage += "<a href=\"" + url + "?p=" + page + "\" class=\"number\">" + page + "</a>";
                } else {
                    break;
                }
            } else {
                if (page > 0) {
                    numberPage += "<span class=\"currentPage\">" + nowPage + "</span>";
                }
            }
        }
        return numberPage;
    }
}
